package cn.doitedu.datayi.data_service.controller;

import cn.doitedu.datayi.data_service.service.SessionQueryService;
import cn.doitedu.datayi.data_service.utils.HbaseConnectionUtil;
import cn.doitedu.datayi.data_service.vo.SessionQueryVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 涛哥
 * @nick_name "deep as the sea"
 * @contact qq:657270652 wx:doit_edu
 * @site www.doitedu.cn
 * @date 2021-01-30
 * @desc 会话信息查询
 */

@RestController
public class SessionQueryController {

    @Autowired
    SessionQueryService sessionQueryService;

    @RequestMapping("/api/v1/session/get")
    public SessionQueryVo query(Integer limit) {

        List<String> data = null;
        try {
            data = sessionQueryService.query(limit);
        } catch (IOException e) {
            e.printStackTrace();
        }

        SessionQueryVo vo = new SessionQueryVo();
        if(data == null){
            vo.setData(null);
            vo.setStatus(-1);
            vo.setMsg("查询失败");
        }else{
            vo.setData(data);
            vo.setStatus(0);
            vo.setMsg("success");
        }

        return vo;
    }

    @RequestMapping("/api/v1/session/getbyprovince")
    public SessionQueryVo query(String province) {

        List<String> data = null;
        try {
            data = sessionQueryService.queryByProvince(province);
        } catch (IOException e) {
            e.printStackTrace();
        }

        SessionQueryVo vo = new SessionQueryVo();
        if(data == null){
            vo.setData(null);
            vo.setStatus(-1);
            vo.setMsg("查询失败");
        }else{
            vo.setData(data);
            vo.setStatus(0);
            vo.setMsg("success");
        }

        return vo;
    }


    @RequestMapping("/api/v1/session/getbycondition")
    public SessionQueryVo queryByCondition(String value) {

        List<String> data = null;
        try {
            data = sessionQueryService.queryByCondition(DigestUtils.md5Hex(value));
        } catch (IOException e) {
            e.printStackTrace();
        }

        SessionQueryVo vo = new SessionQueryVo();
        if(data == null){
            vo.setData(null);
            vo.setStatus(-1);
            vo.setMsg("查询失败");
        }else{
            vo.setData(data);
            vo.setStatus(0);
            vo.setMsg("success");
        }

        return vo;
    }

}
